package com.jservice.web.smkNews;

import com.jservice.framework.dto.Order;
import com.jservice.framework.dto.Pageable;
import com.jservice.framework.dto.Pager;
import com.jservice.framework.idWorker.Sequence;
import com.jservice.framework.spring.SpringContextUtil;
import com.jservice.model.SmkNews;
import com.jservice.web.base.JBaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * SmkNewsQuery
 *
 * @author AutoGenerated
 */
@Service("smkNewsQuery")
@Transactional(readOnly = true)
public class SmkNewsService extends JBaseService{

    public static final SmkNews DAO = new SmkNews();

    public static SmkNewsService me() {
        return SpringContextUtil.getBean("smkNewsQuery");
    }

    @Resource
    private Sequence idWorker;

    public Pager findPage(Pageable pageable) {
        List<Order> orders = new ArrayList<Order>();
        Order order1 = new Order();
        order1.setProperty("status");
        order1.setDirection(Order.Direction.desc);
        Order order2 = new Order();
        order2.setProperty("modify_date");
        order2.setDirection(Order.Direction.desc);
        orders.add(order1);
        orders.add(order2);
        pageable.setOrders(orders);
        return findPage(pageable,DAO, SmkNews.TABLE_NAME);
    }

    @Transactional
    public boolean save(SmkNews news) {

        news.setId(idWorker.nextIdStr());
        news.setCreateDate(new Date());
        news.setModifyDate(news.getCreateDate());
        if (news.getOrders() == null){
            news.setOrders(0);
        }

        return news.save();
    }

    @Transactional
    public boolean update(SmkNews news) {

        news.setModifyDate(new Date());
        if (news.getOrders() == null){
            news.setOrders(0);
        }
        return news.update();
    }
}
